/* Copyright (c) 2008 Erik Beeson
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms,
 * with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * Redistributions of source code must retain the
 *     above copyright notice, this list of conditions
 *     and the following disclaimer.
 * Redistributions in binary form must reproduce the
 *     above copyright notice, this list of conditions
 *     and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 * Neither the name of Erik Beeson nor YourSharade nor the
 *     names of its contributors may be used to endorse or
 *     promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.restly.servlet;

import org.restly.core.ContentType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Arrays;
import java.util.Map;

/**
 * Oct 25, 2008, 8:05:45 AM
 *
 * @author <a href="http://restly.org/">Erik Beeson</a>
 */
public class RestlyHttpServletRequest extends HttpServletRequestWrapper {
	private final Class resourceClass;
	private final Map<String, String> pathAttributes;
	private final String method;
	private final boolean head;

	public RestlyHttpServletRequest(HttpServletRequest request, Class resourceClass, Map<String, String> pathAttributes) {
		super(request);

		this.resourceClass = resourceClass;
		this.pathAttributes = pathAttributes;

		String httpMethod = super.getMethod();
		if("post".equalsIgnoreCase(httpMethod)) {
			String s = request.getParameter("method-override");
			httpMethod = ("put".equalsIgnoreCase(s) || "delete".equalsIgnoreCase(s)) ? s : httpMethod;
			s = request.getHeader("X-HTTP-Method-Override");
			httpMethod = ("put".equalsIgnoreCase(s) || "delete".equalsIgnoreCase(s)) ? s : httpMethod;
		}

		if("head".equalsIgnoreCase(httpMethod)) {
			httpMethod = "get";
			head = true;
		} else {
			head = false;
		}

		method = httpMethod.toLowerCase();
	}

	public ContentType getPreferredContentType() {
		return ContentType.getBestContentType(getHeader("Accept"), Arrays.asList(ContentType.TEXT_XML, ContentType.JSON));
	}

	public String getMethod() {
		return method;
	}

	public Class getResourceClass() {
		return resourceClass;
	}

	public String getResourceName() {
		String s = resourceClass.getSimpleName().replaceAll("Resource$", "");
		return s.substring(0, 1).toLowerCase() + s.substring(1);
	}

	public boolean isHead() {
		return head;
	}

	public boolean isOptions() {
		return "options".equals(getMethod());
	}

	public boolean isGet() {
		return "get".equals(getMethod());
	}

	public boolean isPost() {
		return "post".equals(getMethod());
	}

	public boolean isPut() {
		return "put".equals(getMethod());
	}

	public boolean isDelete() {
		return "delete".equals(getMethod());
	}

	public boolean isWrite() {
		return isPost() || isPut() || isDelete();
	}

	public Map<String, String> getPathAttributesMap() {
		return pathAttributes;
	}
}
